/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.model;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import za.org.foobar.famon.util.DB;

import javax.jdo.annotations.*;

/**
 *
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class ApplicationGroup {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key id;
    @Persistent
    private int version;

    @Persistent
    private Key applicationId;
    @NotPersistent
    private Application application;
    @Persistent
    private Key applicationInstanceId;
    @NotPersistent
    private ApplicationInstance applicationInstance;
    @Persistent
    private Key applicationVersionId;
    @NotPersistent
    private ApplicationVersion applicationVersion;
    @NotPersistent
    private long failureCount;

    public ApplicationGroup() {
    }

    public ApplicationGroup(Key applicationId, Key applicationInstanceId, Key applicationVersionId) {
        this.applicationId = applicationId;
        this.applicationInstanceId = applicationInstanceId;
        this.applicationVersionId = applicationVersionId;
    }

    public Key getId() {
        return id;
    }

    public void setId(Key id) {
        this.id = id;
    }

    public Key getApplicationId() {
        return applicationId;
    }

    public void setApplicationId(Key applicationId) {
        this.applicationId = applicationId;
    }

    public Key getApplicationInstanceId() {
        return applicationInstanceId;
    }

    public void setApplicationInstanceId(Key applicationInstanceId) {
        this.applicationInstanceId = applicationInstanceId;
    }

    public Key getApplicationVersionId() {
        return applicationVersionId;
    }

    public void setApplicationVersionId(Key applicationVersionId) {
        this.applicationVersionId = applicationVersionId;
    }

    public Application getApplication() {
        if (application == null) {
            if (applicationId == null) {
                return null;
            }
            application = DB.findEx(Application.class, applicationId);
        }
        return application;
    }

    public ApplicationVersion getApplicationVersion() {
        if (applicationVersion == null) {
            if (applicationVersionId == null) {
                return null;
            }
            applicationVersion = DB.findEx(ApplicationVersion.class, applicationVersionId);
        }
        return applicationVersion;
    }

    public ApplicationInstance getApplicationInstance() {
        if (applicationInstance == null) {
            if (applicationInstanceId == null) {
                return null;
            }
            applicationInstance = DB.findEx(ApplicationInstance.class, applicationInstanceId);
        }
        return applicationInstance;
    }

    public long getFailureCount() {
        return failureCount;
    }

    public void setFailureCount(long failureCount) {
        this.failureCount = failureCount;
    }

    public String getTitle() {
        return getApplication().getName() + " - " + getApplicationInstance().getName() + " - "
                + getApplicationVersion().getName(); 
    }

    public String keyToString() {
        return keyToString(id);
    }

    public static String keyToString(Key id) {
        return Long.toString(id.getId());
    }

    public static Key stringToKey(String id) {
        return KeyFactory.createKey(ApplicationGroup.class.getSimpleName(), Long.parseLong(id));
    }
}
